package com.jiangcheng.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangcheng.demo.entity.Article;
import com.jiangcheng.demo.mapper.ArticleMapper;
import com.jiangcheng.demo.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangcheng.demo.utils.FindArticleByAuthorIdVo;
import com.jiangcheng.demo.utils.FindArticleVo;
import com.jiangcheng.demo.utils.InsertReturnArticleVo;
import com.jiangcheng.demo.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-07-18
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    ArticleMapper articleMapper;

    @Override
    public Result findArticleByPage(int page,int pageSize,int filterUId) {
        List<FindArticleVo> articleByPage = articleMapper.findArticleByPage((page - 1) * pageSize, pageSize,filterUId);
        for (FindArticleVo findArticleVo:articleByPage){
            System.out.println(findArticleVo);
        }

        if (articleByPage.isEmpty()){
            return new Result(400,false,"没有更多日记了",articleByPage);
        }

        return new Result(200,true,"获取日记信息成功",articleByPage);
    }

    // 根据author_id 查询文章
    public Result findArticleByAuthorId(int authorId){
        System.out.println("Service:" + authorId);
        List<Article> data = articleMapper.findArticleByAuthorId(authorId);
        Integer count = articleMapper.findCountByAuthorId(authorId);
        if (data.size() == 0){
            return new Result(400,false,"暂无更多日记");
        }
        FindArticleByAuthorIdVo vo = new FindArticleByAuthorIdVo();
        vo.setArticleCount(count);
        vo.setData(data);
        return new Result(200,true,"获取日记成功", vo);
    }

    @Override
    public Result findArticleByLike(String search) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", search).or().like("content", search);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        if (articles.size() == 0){
            return new Result(400,false,"查无结果");
        }
       return new Result(200,true,"模糊查询成功", articles);
    }

    @Override
    public Result InsertArticle(Article article) {
        int insert = articleMapper.insert(article);
        System.out.println("插入数据的条数:" + insert);
        // 尝试获取ID
        Integer artId = article.getArtId();
        InsertReturnArticleVo vo = new InsertReturnArticleVo();
        vo.setArtId(artId);
        vo.setArticlePic(null);
        vo.setAuthorId(article.getAuthorId());
        vo.setTitle(article.getTitle());
        vo.setContent(article.getContent());

        if (insert != 1){
            return new Result(400,false,"发布日记失败");
        }

        return new Result(200,true,"发布日记成功",vo);
    }

    @Override
    public Result UpdateArticle(Article article) {
        System.out.println("ID:" + article);
        int i = articleMapper.updateById(article);
        if (i != 1){
            return new Result(400,false,"修改失败");
        }
        return new Result(200,true,"编辑成功");
    }

    @Override
    public Result findArticleInfoById(int artId) {
        FindArticleVo data = articleMapper.findArticleInfoById(artId);
        if (data == null){
            return new Result(400,false,"查询失败");
        }
        return new Result(200,true,"加载成功",data);
    }

    @Override
    public Result deleteArticleById(int artId) {
        System.out.println("artId: " + artId);
        Integer delete_num = articleMapper.deleteArticleById(artId);

        if (delete_num == null){
            return new Result(400,false,"删除失败！");
        }
        return new Result(200,true,"删除成功！");
    }

    @Override
    public Result findArticleById(int artId) {
        Article article = articleMapper.selectById(artId);

        if (article == null){
            return new Result(400,false,"查询失败");
        }

        return new Result(200,true,"查询成功",article);
    }


}
